﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Sharp.DataTransferObject.Workflow;
using Sharp.Domain.PlatformBoundedContext.WorkFlowModule.Aggregates;
using Sharp.Domain.PlatformBoundedContext.WorkFlowModule.Engine.Common;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Search;

namespace Sharp.Application.Platform
{
    public partial class WorkflowServiceImpl
    {    /// <summary>
         /// 获取待处理流程实例分页数据
         /// </summary>
         /// <param name="pf"></param>
         /// <returns></returns> 
        public PagedResult<TaskDTO> GetReadyTasksPaged(PagingFeature pf)
        {
            var userId = HttpContext.Current.User.Identity.GetUserId().ToString();
            var list = _taskEntityRepository.Pagging(pf,
                    x => x.AssignedToUserID == userId && x.TaskState == TaskStateEnum.Waiting
                    , x => x.ProcessInstance
                    )
                .To<TaskEntity, TaskDTO>();

            list.Data.ForEach(x =>
            {
                x.ProcessInstance.ProcessStateText = ((ProcessStateEnum)x.ProcessInstance.ProcessState).GetDescription();
            });

            return list;
        }
        /// <summary>
        /// 获取已完成流程实例分页数据
        /// </summary>
        /// <param name="pf"></param>
        /// <returns></returns> 
        public PagedResult<TaskDTO> GetCompletedTasksPaged(PagingFeature pf)
        {
            var userId = HttpContext.Current.User.Identity.GetUserId().ToString();
            var list = _taskEntityRepository.Pagging(pf,
                x => x.AssignedToUserID == userId && x.TaskState == TaskStateEnum.Completed,
                x => x.ProcessInstance).To<TaskEntity, TaskDTO>();

            list.Data.ForEach(x =>
            {
                x.ProcessInstance.ProcessStateText = ((ProcessStateEnum)x.ProcessInstance.ProcessState).GetDescription();
            });

            return list;
        }
    }
}
